package gk.cxd.friday.repository.custom.impl;

import gk.cxd.friday.repository.custom.SysUserCustomRepository;
import gk.cxd.friday.utils.SqlUtil;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;


@Service
public class SysUserCustomRepositoryImpl implements SysUserCustomRepository {

    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public int batchInsertUserRole(Long[] userIds, Long[] roles) {
        int length = userIds.length > roles.length ? roles.length:userIds.length;
        StringBuffer sql = new StringBuffer();
        sql.append(" insert into sys_user_role(user_id,role_id) values ");
        sql.append(SqlUtil.getBatchInsertSqlStr(length,2));
        Query query = entityManager.createQuery(sql.toString());
        int paramIndex = 1;
        for(int i = 0;i<length;i++){
            query.setParameter(paramIndex++,userIds[i]);
            query.setParameter(paramIndex,roles[i]);
        }
        //执行数据库语句
        return query.executeUpdate();
    }

}
